const sequelize = require('../config/database');
const logger = require('../utils/logger');

async function checkIndexes() {
  const queryInterface = sequelize.getQueryInterface();
  
  try {
    // 获取 admins 表的所有索引
    const indexes = await queryInterface.showIndex('admins');
    
    // 需要保留的索引名称
    const keepIndexes = ['PRIMARY', 'admins_username', 'admins_status'];
    
    // 找出多余的索引
    const extraIndexes = indexes
      .filter(index => !keepIndexes.includes(index.name))
      .map(index => index.name);
    
    if (extraIndexes.length > 0) {
      logger.warn(`警告: 发现 ${extraIndexes.length} 个可能多余的索引: ${extraIndexes.join(', ')}`);
      logger.warn('请手动检查这些索引是否需要删除。可以使用以下SQL删除多余的索引:');
      logger.warn(`ALTER TABLE admins ${extraIndexes.map(name => `DROP INDEX ${name}`).join(', ')};`);
    } else {
      logger.info('没有发现多余的索引');
    }
    
    return extraIndexes;
  } catch (error) {
    logger.error('检查索引时出错:', error);
    throw error;
  }
}

// 如果直接运行脚本则执行检查
if (require.main === module) {
  checkIndexes()
    .then(() => process.exit(0))
    .catch(error => {
      console.error(error);
      process.exit(1);
    });
}

module.exports = checkIndexes; 